import numpy as np
import matplotlib.pyplot as plt

# 示例数据
labels = ["Group 1", "Group 2", "Group 3"]
data1 = [10, 20, 30]
data2 = [15, 25, 35]
data3 = [12, 22, 32]

x = np.arange(len(labels))  # 标签位置
width = 0.25  # 柱子的宽度

fig, ax = plt.subplots()
rects1 = ax.bar(x - width, data1, width, label="Data 1")
rects2 = ax.bar(x, data2, width, label="Data 2")
rects3 = ax.bar(x + width, data3, width, label="Data 3")

# 添加标签、标题等
# ax.set_ylabel('Values')
# ax.set_title('Grouped Bar Chart')
# ax.set_xticks(x)
# ax.set_xticklabels(labels)
ax.legend()


# 选项opt: 在每个柱子上方添加数值标签
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate(
            "{}".format(height),
            xy=(rect.get_x() + rect.get_width() / 2, height),
            xytext=(0, 3),  # 3 points vertical offset
            textcoords="offset points",
            ha="center",
            va="bottom",
        )


autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

# 选项opt: 调整布局，避免重叠
plt.tight_layout()
plt.show()
